﻿using DalatCooking.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DalatCooking.Core.Helper;

public partial class UserControls_UserAccount : System.Web.UI.UserControl
{
    public Person p;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Page.Title = "Tài khoản";
        p = Session.GetPersonLogged();
       
        if (p == null) Session.Abandon();
        LoadInfo();

        rptMyCourse.DataSource = CourseManager.CourseOfPerson(p.PersonId);
        rptMyCourse.DataBind();
    }

    void LoadInfo()
    {
        Person pr = PersonManager.FindById(p.PersonId);
        txtFirstName.Text = pr.Profile.FirstName;
        txtLastName.Text = pr.Profile.LastName;
        txtDisplayName.Text = pr.Profile.DisplayName;
        txtDateOfBirth.Text = pr.Profile.DateOfBirth.ToString();
        txtAvatar.Text = pr.Profile.Avatar;
        txtAbout.Text = pr.Profile.AboutMe;
    }
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("/account");
    }
    protected void btnChangePass_Click(object sender, EventArgs e)
    {
        Person p = Session.GetPersonLogged();
        p.Password = txtPassword.Text.Trim();
        p.PasswordNew = txtPasswordNew2.Text.Trim();

        if (PersonManager.ChangePassword(p))
        {
            divStatus.InnerHtml = "Thay đổi mật khẩu thành công";
            divStatus.Attributes.Add("class", "success");
        }
        else
        {
            divStatus.InnerHtml = "Thay đổi mật khẩu thất bại";
            divStatus.Attributes.Add("class", "errors");
        }
    }
    protected void btnSaveProfile_Click(object sender, EventArgs e)
    {
        Profile pr = new DalatCooking.Core.Profile();
        pr.PersonID = p.PersonId;
        pr.FirstName = txtFirstName.Text;
        pr.LastName = txtLastName.Text;
        pr.DisplayName = txtDisplayName.Text;
        pr.DateOfBirth = ConvertObject.ToDateTime(txtDateOfBirth.Text);
        pr.Avatar = txtAvatar.Text;
        pr.AboutMe = txtAbout.Text;
        pr.Email = p.Email;
        pr.AddressId = Session.GetPersonLogged().Profile.AddressId;
        pr.IsPrivate = false;
        pr.MiddleName = string.Empty;
        if (ProfileManager.Update(pr))
            Response.Redirect(Request.RawUrl);
    }
}